package com.s666.demo.service.impl;

//import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.s666.demo.dao.mapper.ZikuMapper;
import com.s666.demo.dao.entity.Ziku;
import org.springframework.stereotype.Service;
import com.s666.demo.service.ZikuService;

import javax.annotation.Resource;
import java.util.List;
/**
 * @author lijin
 */
@Service
public class ZikuServiceImpl extends ServiceImpl<ZikuMapper, Ziku> implements ZikuService {

    @Resource
    private ZikuMapper zikuMapper;

    /**
     * 获取总数
     * @param ziku
     * @return
     */
    @Override
    public long count(Ziku ziku){
        EntityWrapper<Ziku>  entityWrapper =  this.query(ziku);
        long num = zikuMapper.selectCount(entityWrapper);
        return num;
    }

    /**
     * 获取列表
     * @param ziku
     * @param startRow
     * @param pageSize
     * @return
     */
    @Override
    public  Page<Ziku> listZiku(Ziku ziku, int startRow, int pageSize){
        //PageHelper.startPage(startRow > 0 ? startRow : 1, 10);

        EntityWrapper<Ziku>  entityWrapper =  this.query(ziku);
        entityWrapper.orderBy("id",true);

        Page<Ziku> pager = new Page<Ziku>(startRow > 0 ? startRow : 1, pageSize);
        List<Ziku> zikuList = zikuMapper.selectPage( pager, entityWrapper );
        return pager.setRecords( zikuList );
    }

    /**
     * 组合查询条件
     * @param ziku
     * @return
     */
    private  EntityWrapper<Ziku> query(Ziku ziku){
        EntityWrapper<Ziku> entityWrapper  =  new EntityWrapper<Ziku>();
        String py = ziku.getPy();
        if ( py != null && !"".equals(py) ){
            entityWrapper.eq("py", py);
        }

        String letter = ziku.getLetter();
        if (letter != null && !"".equals(letter)){
            letter = letter.toLowerCase();
            entityWrapper.eq("letter", letter);
        }

        String zi = ziku.getZi();
        if (zi != null && !"".equals(zi)){
            entityWrapper.eq("zi", zi);
        }

        Integer type = ziku.getType();
        if (type != null){
            entityWrapper.eq("type", type);
        }

        Boolean frequency = ziku.getFrequency();
        if (frequency != null){
            entityWrapper.eq("frequency", frequency);
        }

        return entityWrapper;
    }

    @Override
    public int saveEntity(Ziku ziku) {

        int result = zikuMapper.insertSelective(ziku);
        return result;
    }

}
